<template>
	<div class="system-menu-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="500px">
			<el-form ref="addGradeDialogFormRef" :rules="state.rules" :model="state.ruleForm"  label-width="80px">
				<el-row :gutter="35">
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="等级名称">
							<el-input v-model="state.ruleForm.name" placeholder="请输入等级名称" clearable></el-input>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="等级级别">
							<el-input v-model="state.ruleForm.grade" placeholder="请输入等级级别" clearable></el-input>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="享受折扣">
                            <el-input v-model="state.ruleForm.discount" placeholder="请输入等级折扣" clearable>
                                <template #append>折</template>
                            </el-input>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="经验">
							<el-input v-model="state.ruleForm.experience" placeholder="请输入经验值" clearable></el-input>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="图标">
							<el-image style="width: 100px; height: 100px" :src="state.ruleForm.icon" fit="cover" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel"  >取 消</el-button>
					<el-button type="primary" @click="onSubmit"  >{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="addGradeDialog">
import { levelSave, levelUpdate } from "/@/api/member";
import { reactive, onMounted, ref } from 'vue';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 定义变量内容
const addGradeDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id: '',
		name: '', // 等级名称
		experience:'', // 经验值 
		grade: '', // 等级
		discount: '', // 折扣 百分比
		image: '', // 图片
		icon: '', // 图标
		memo: '',
		createTime: '',
		updateTime: '',
		storeId: '',
		del: '',
		show: ''
	},
	rules: {
		name: { required: true, message: '请输入等级名称', trigger: 'blur' },
		experience: { required: true, message: '请输入经验值', trigger: 'blur' },
		grade: { required: true, message: '请输入等级', trigger: 'blur' },
		discount: { required: true, message: '请输入折扣百分比', trigger: 'change' },
		icon: { required: true, message: '请选择图片', trigger: 'change' },
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});


// 打开弹窗
const openDialog = (type: string, row?: any) => {
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.dialog.title = '会员等级编辑';
		state.dialog.submitTxt = '修 改';
	} else {
		state.ruleForm = {
			id: '',
			name: '', // 等级名称
			experience:'', // 经验值 
			grade: '', // 等级
			discount: '', // 折扣 百分比
			image: '', // 图片
			icon: '', // 图标
			memo: '',
			createTime: '',
			updateTime: '',
			storeId: '',
			del: '',
			show: ''
		},
		state.dialog.title = '新增会员等级';
		state.dialog.submitTxt = '新 增';
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async () => {
	if(state.dialog.type == 'edit'){
	// 编辑
		let res = await levelUpdate(state.ruleForm);
		console.log(res)
	}else{
		let res = await levelSave(state.ruleForm);
		console.log(res)
	}
	closeDialog(); // 关闭弹窗
	emit('refresh');
	
	// if (state.dialog.type === 'add') {}
	// setBackEndControlRefreshRoutes() // 刷新菜单，未进行后端接口测试
};
// 页面加载时
onMounted(() => {

});

// 暴露变量
defineExpose({
	openDialog,
});
</script>
